<?php
require_once(APPPATH.'views/components/page.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='<?php echo base_url() ?>resources/css/admin.css' rel='stylesheet' type='text/css' />
		<script src="<?php echo base_url() ?>resources/js/jquery-1.4.2.min.js"></script>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		<script type="text/javascript">
			window.onbeforeunload = leavingPageHandler;
		    function leavingPageHandler(e)
		    {
		         // Reset check boxes
		         var selectall = document.getElementsByName("selectall");
		         selectall[0].checked = false;
		         onSelectAll(selectall[0]);
		    }
		    
		    function showAddBtn(show)
			{
				var addBtn = document.getElementById("addbtn");
				addBtn.disabled = !show;
			}
		    
		    function onSelectAll(checkbox)
		    {
		    	var checkboxes = document.getElementsByName("products[]");
				for (var i = 0; i < checkboxes.length; i++)
				{
					checkboxes[i].checked = checkbox.checked;
				}
				
				showAddBtn(checkbox.checked);
		    }
		    
		    function singleCbClicked()
			{
				var checkboxes = document.getElementsByName("products[]");
				var count = 0;
				for (var i = 0; i < checkboxes.length; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				showAddBtn(count > 0);
				
				var selectall = document.getElementsByName('selectall');
				selectall[0].checked = (count == checkboxes.length);
					
			}
			
			function onAddBtnClicked(label)
			{
				var checkboxes = document.getElementsByName("products[]");
				var count = 0;
				
				for (var i = 0, size = checkboxes.length; i < size; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				
				if (count == 0)
					return false;
					
				return confirm("Thêm " + count + " sản phẩm vào nhãn '" + label + "'?");
			}
		</script>
		
		<title><?php echo $mark->getTitle() ?></title>
	</head>
	
	<body>
		<div id='wrapper'>
			<div id='header'>
				<div id="leftTopPan">
				
				</div>
				<h1></h1>
			</div>
			<div id="middle">
				<div id="container">
					<div id="content">
						<br /><h2 align="center"><?php echo $mark->getTitle() ?></h2><br />
						
						<?php
						$userdata = $this->session->userdata('userdata');
						// Error handler
						$error = $this->session->userdata('error');
						$this->session->unset_userdata('error');
						
						if (isset($rerror))
							$error = $rerror;
						
						$errorText = "";
						
						switch ($error) {
							case INSERT_ERROR:
								$errorText = ADD_PRODUCTS_TO_MARK_ERROR_TEXT;
								break;
							case NOT_EXISTED_ERROR:
								$errorText = NOT_EXISTED_ERROR_TEXT;
								break;
							default:
								break;
						}
						?>
						<h3 style="color: red"><?php echo $errorText ?></h3>
						
						<?php
						
						if (Page::getInstance()->isAdmin() && $mark->getId() > 0)
						{
							// User information
							Page::getInstance()->generateUserInfo($userdata);
							
							// Add mark to products
							echo anchor('mark/viewproducts/'.$mark->getId(), 'Quay lại');
							
							// Product list
							echo "<br/><br/>";
							echo form_fieldset('Danh sách sản phẩm');
							
							// Begin add form
							echo form_open('mark/processaddproducts');
							
							// Hidden
							echo form_hidden('markid', $mark->getId());
							
							//Delete button
							$deleteBtn = array(	'id'	=>	'addbtn',
												'name'	=>	'addbtn',
												'value'	=>	'Thêm vào nhãn này',
												'disabled'=> TRUE,
												'onclick'	=> "return onAddBtnClicked('".$mark->getTitle()."')");
							echo form_submit($deleteBtn);
							
							$js = "onclick='onSelectAll(this)'";
					?>
						<table id='producttable' border='1'>
							<tr>
								<td width='2%'><?php echo form_checkbox('selectall', '', FALSE, $js) ?></td>
								<th width='40%'>Tên</th>
								<th witdh='25%'>Nhóm</th>
								<th width='10%'>Xuất xứ</th>
								<th width='15%'>Nhãn</th>
								<th width='8%'>Giá</th>
								<th>Khuyến Mãi</th>
							</tr>
					<?php
							$js_show = "onclick='singleCbClicked()'";
							foreach ($products as $product)
							{
					?>
							<tr>
								<td><?php echo form_checkbox('products[]', $product->getId(), FALSE, $js_show) ?></td>
								<td><?php echo anchor('product/edit/'.$product->getId(), $product->getName()) ?></td>
								<td><?php echo $product->getProductType()->getTitle() ?></td>
								<td><?php echo $product->getBranch()->getName() ?></td>
								<td>
								<?php
								foreach($product->getMarks() as $mark)
								{
									echo anchor('mark/viewproducts/'.$mark->getId(), $mark->getTitle());
									echo "<br/>";
								}
								?>
								</td>
								<td><?php echo $product->getPrice() ?></td>
								<td>
									<?php 
									if ($product->isPromotion())
										echo "Yes";
									else
										echo "No";
									?>
								</td>
							</tr>
					<?php
							}
					?>
						</table>
						<div id="pagination">
					<?php
						if (isset($pagination))
							echo $pagination;
					?>
						</div>
						
					<?php
							// Close add form
							echo form_close();
							
							echo form_fieldset_close();
						}
						?>
					</div>
				</div>
					<?php
						if (Page::getInstance()->isAdmin())
						{
							Page::getInstance()->generateAdminLeftMenu();
						}
					?>
			</div>
		</div>
		
	</body>
</html>